package com.css.fxfzaqbz.modules.assess2.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess2.entity.KrTask;
import com.css.fxfzaqbz.modules.assess2.entity.UTask;
import com.css.fxfzaqbz.modules.assess2.repository.KrTaskRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class KrTaskRepositoryImpl implements KrTaskRepository {
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Override
    public void saveKrTask(KrTask krTask) {
        String sql = "insert into sys_kr_task (kt_id,kt_activeid,kt_opinion,kt_status,kt_create_time," +
                "kt_create_user,kt_del_flag,content) values(:ktId,:ktActiveid,:ktOpinion,:ktStatus,:ktCreateTime," +
                ":ktCreateUser,:ktDelFlag,:content)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(krTask);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public KrTask getTask(String activeId) {
        try {
            String sql="select * from sys_kr_task where kt_activeid='"+activeId+"' and kt_del_flag='"+ YNEnum.N+"'";
            KrTask krTask = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(KrTask.class));
            return krTask;
        } catch (DataAccessException e) {
            return null;
        }


    }

    @Override
    public void updateBigStatus(String bbId) {
        String sql = "update sys_kr_task set" +
                " kt_status = '"+YNEnum.Y+"' " +
                " where kt_id = '" + bbId + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void upStatus(String id) {
        String sql = "update sys_kr_task set" +
                " kt_status = '"+2+"' " +
                " where kt_activeid = '" + id + "'";
        jdbcTemplate.update(sql);
    }
}
